import switchTab from "../commonComponents/switchTab.vue"

export default {
  data() {
    return {
      topNavFixedShow: false,
      homeWarnFlag: false,
      homeInitFLag: false,
      switchFlag: 0,
      tabChecked: 99,
      routerAn: "slideRight",
      tabData: [],
      navList: {},
      homeInfoList: [],
      homePushList: [],
      homeWarnList: [],
      riskPageIndex: 0,
      infoPageIndex: 0,
      pushPageIndex: 0,
      titleSc: "titleSc",
      headerOlAn: "headerOlAn",
      riskCount: false,
      // headerImg:`url(${require("../../img/gxb.jpg")})`,
      headerImg:_data.logoUrl,
      showControl: {},
    }
  },
  methods: {
    init(refresh) {
      if (!refresh) {
        setTimeout(() => {
          this.homeScrollInit();
        }, 100);
      }
      else {
        this.headerOlAn = "headerOlAn";
        this.riskPageIndex = 0;
        this.infoPageIndex = 0;
        this.pushPageIndex = 0;
        this.homeInfoList = [];
        this.homePushList = [];
        this.homeWarnList = [];
        if (window._homeData.showControl.first.indexOf(window._data.userInfo.roleRank) != -1) {
          this.getInfoList(true, 2);
          this.riskListGet(true, false);
        }
        if (window._homeData.showControl.sec.indexOf(window._data.userInfo.roleRank) != -1) {
          this.getInfoList(true, 1);
        }
      }
      this.navCountGet();
      this.showControlGet();
    },
    homeScrollInit() {
      let self = this;
      window.homeScroll = _methods.initMeScroll("homeScroll", {
        //刷新
        down: {
          callback: () => {
            self.init(true);
          }
        },
        //加载更多
        up: {
          onScroll: function (meScroll, y) {
            if (y >= 209) {
              if (!self.topNavFixedShow) {
                self.topNavFixedShow = true;
              }
            } else {
              if (self.topNavFixedShow) {
                self.topNavFixedShow = false;
              }
            }
          },
          callback: () => {
            let fn = (id) => {
              switch (id) {
                case 322:
                  self.getInfoList(false, 2);
                  break;
                case 323:
                  self.getInfoList(false, 1);
                  break;
                case 324:
                  self.riskListGet(false, true);
                  break;
              }
            };
            if (_data.userInfo.roleRank == 6){
              homeScroll.endSuccess(9, 9 == 10);
            }
            if (!self.homeInitFLag) {
              self.tabData.forEach((item) => {
                fn(item.id);
              });
              self.homeInitFLag = true;
            } else {
              fn(self.tabChecked.id);
            }
          },
        }
      });
    },
    animateClassRemove() {
      setTimeout(() => {
        this.titleSc = "";
        this.headerOlAn = "";
      }, 60);
    },
    navScrollInit(){
      mui('#navScroll').scroll({
        scrollY: false, //是否竖向滚动
        scrollX: true, //是否横向滚动
        deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
      });
    },
    navClick(item) {
      _methods.goPath(item.path);
    },
    //导航获取
    navCountGet() {
      _ajax.noLoading(_ajaxUrls.homeCount.url,
        {},
        (ajData) => {
          let aj = ajData.result;
          let nl = [];
          if (_data.userInfo.roleRank == 6){
            window._homeData.navList["301319"].path = "/riskList?type=9";
          }
          for (let key in window._homeData.navList) {
            let vl = window._homeData.navList[key];
            if (vl.role.indexOf(window._data.userInfo.roleRank.toString()) != -1) {
              let tc = aj.taskCount[vl.num.key];
              let l = aj[vl.num.key];
              if (tc || tc == 0) {
                if (parseInt(tc) > 99) {
                  tc = 99;
                }
                vl.num.value = tc;
              }
              if (l || l == 0) {
                if (parseInt(l) > 99) {
                  tc = 99;
                }
                vl.num.value = l;
              }
              nl.push(vl)
            }
          }
          this.navList = nl;
          this.riskCount = aj.warningCount;
          this.animateClassRemove();
          setTimeout(()=>{
            this.navScrollInit();
          },60);
        },
        _ajaxUrls.homeCount.type);
    },
    tabDataClick(i, item) {
      this.switchFlag = i;
      this.tabChecked = item;
    },
    riskListGet(isRefresh, scroll = false) {
      // pageIndex：当前页码，大于等于1
      // pageSize	：每页数据条数， 默认20
      // level：等级:-1位全部
      // type：类型：1事件2预警
      this.riskPageIndex = this.riskPageIndex + 1;

      let data = {
        userID: window._data.userInfo.userId,
        size: window._data.pageSize,
        timeType: 1,
        status: 1,
        page: this.riskPageIndex,
      };

      let listFormat = (arr) => {
        return arr.map(i => {
          i.warnRiskLevelClass = `l${i.warningLevel}`;
          return i;
        })
      };

      _ajax.noLoading(_ajaxUrls.riskManage.url,
        data,
        (ajData) => {
          let arr = listFormat(ajData.result.records);
          if (isRefresh) {
            this.homeWarnList = arr;
          } else {
            let rd = this.homeWarnList;
            arr.forEach(i => {
              rd.push(i);
            });
            this.homeWarnList = rd;
          }
          if (scroll) {
            homeScroll.endSuccess(arr.length, arr.length == _data.pageSize);
          }
        }, _ajaxUrls.riskManage.type);
    },
    goWarnDetail(item) {
      _methods.goPath(`/riskList/riskDetail?id=${item.articleID}&listType=1&type=3&flag=1`);
    },
    goInfoDetails(item) {
      _methods.goPath(`/home/homeInfoDetail?id=${item.id}&type=2`);
    },
    getInfoList(isRefresh, type = 1) {

      // pageIndex：当前页码
      // pageSize：每页大小
      // type:1信息推送 2今日快报
      let pageKey = {
        1: "pushPageIndex",
        2: "infoPageIndex",
      }[type];

      this[pageKey] = this[pageKey] + 1;

      let data = {
        page: this[pageKey],
        size: window._data.pageSize,
      };
      let key = {
        1: "homePushList",
        2: "homeInfoList",
      }[type];

      _ajax.noLoading(_ajaxUrls.homeInformationList.url,
        data,
        (ajData) => {
          let arr = ajData.result.records;
          let rd = [];
          if (!isRefresh) {
            rd = this[key];
          }
          arr.forEach(i => {
            rd.push(i);
          });
          this[key] = rd;
          homeScroll.endSuccess(arr.length, arr.length == _data.pageSize);
        }, _ajaxUrls.homeInformationList.type);
    },
    showControlGet() {
      let arr = [];
      _data.userInfo.isCenterInit(() => {
        arr = [
          {id: 322, txt: "安全动态"},
          {id: 324, txt: "当前预警"}
        ];
      }, () => {
        if (_data.userInfo.roleRank != 6 && _data.userInfo.roleRank != 5){
          arr = [
            {id: 323, txt: "安全动态"}
          ];
        }
      });
      this.tabData = arr;
      if (arr.length) {
        this.tabChecked = arr[0];
      }
    },
  },
  mounted() {
    window._vrvInit.init().then(() => {
      this.init(false);
    });
  },
  components: {
    switchTab
  },
}
